//
//  SQChangeOrderTableViewCell.m
//  pai
//
//  Created by pro on 14-12-8.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "SQChangeOrderTableViewCell.h"
#import "ASIHTTPRequest.h"
#import "UIImageView+WebCache.h"
#import "Global.h"

@implementation SQChangeOrderTableViewCell

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)setItem:(order_t)order
{
    //商品图片
    if (self.imageUrl == nil) {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/skus/%@/photos.json", IMAGESERVICE,[NSString stringWithUTF8String: order.skucode]]];
        NSLog(@"get image url:%@",url);
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        
        [request startSynchronous];
        
        NSError *error = [request error];
        
        if (!error) {
            
            NSString *response = [request responseString];
            NSLog(@"%@",response);
            
            NSData *data= [response dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error = nil;
            NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
            //        NSLog(@"%@",dic);
            //        NSLog(@"home:%@",[dic objectForKey:@"home"]);
            //        NSMutableArray* array =[dic objectForKey:@"intro"];
            //        NSLog(@"count:%d",array.count);
            NSString* imageUrl = [NSString stringWithFormat:@"%@%@",IMAGESERVICE,[dic objectForKey:@"home"]];
            self.imageDic = dic;
            self.imageUrl =imageUrl;
        }
    }
    self.imgView.contentMode = UIViewContentModeScaleAspectFit;
    [self.imgView setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
    //拍品名称
    self.nameLabel.text = [NSString stringWithUTF8String:order.skuname];
    //出价价格
    NSString* pricestr = [NSString stringWithFormat:@"%d",order.price];
    NSString* pointsstr = [NSString stringWithFormat:@"%d",order.points];
    NSMutableAttributedString* price;
    if (order.price!=0&&order.points==0) {
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@豆",pricestr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pricestr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-1,1)];
    }else if (order.price==0&&order.points!=0){
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@积分",pointsstr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pointsstr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-2,2)];
    }else{
        price = [[NSMutableAttributedString alloc]initWithString:[[NSString alloc] initWithFormat:@"使用了：%@豆+%@积分",pricestr,pointsstr]];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,4)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(4,pricestr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(pricestr.length+4,2)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(pricestr.length+6,pointsstr.length)];
        [price addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(price.length-2,2)];
    }
    self.contentLabel.attributedText = price;
}
@end
